package com.utility.project.schedule;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.utility.project.entity.YsCustomer;
import com.utility.project.service.IYsCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class CustomerUsageSchedule {

    @Resource
    IYsCustomerService iYsCustomerService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void updateDailyFreeUsage() {
        List<YsCustomer> ysCustomerList = iYsCustomerService.queryCustomerByFreeUsage();
        if(CollUtil.isNotEmpty(ysCustomerList)) {
            log.info("开始更新每日清空用户的免费使用次数,userNameList:{}", JSONUtil.toJsonStr(ysCustomerList.stream().map(YsCustomer::getId).toArray()));
            for (YsCustomer ysCustomer : ysCustomerList) {
                ysCustomer.setFreeUsageTimes(0);
            }
            iYsCustomerService.updateBatchById(ysCustomerList);
        }
    }

    @Scheduled(cron = "0 */5 * * * ?")
    public void updateInvalidCustomerUsage() {
        List<YsCustomer> ysCustomerList = iYsCustomerService.queryInvalidCustomer();
        if(CollUtil.isNotEmpty(ysCustomerList)) {
            log.info("开始更新过期会员的使用次数,userNameList:{}", JSONUtil.toJsonStr(ysCustomerList.stream().map(YsCustomer::getUsername).toArray()));
            for (YsCustomer ysCustomer : ysCustomerList) {
                ysCustomer.setMemberUsageTimes(0);
            }
            iYsCustomerService.updateBatchById(ysCustomerList);
        }
    }

}
